<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\index\controller;
use app\common\model\User as U;
use app\common\model\Ceshiexam as CE;
use app\common\model\Shiti as ST;
use think\Db;

/**
 * 前台首页控制器
 * @package app\index\controller
 */
class Index extends Home
{
    public function login()
    {
        $username = input('username');
        $phone = input('phone');
		$user = U::where(['username' => $username])->find();
		if (!$user) output_error('账号不存在');

		if ($user['phone'] != $phone) output_error('姓名和手机不匹配');
		output_data($user,'登录成功');
    }

    public function jinrukaoshi(){
    	if(time()>config('web_daojishi')){
    		output_error('超过了答题时间，入口已关闭');
    	}else{
    		output_data('进入考试');
    	}
    }

    public function tijiao(){
    	if(empty(input('user_id')))output_error('请先登录');

    	$data = input();
    	$data['create_time'] = time();
    	if(Db::name('exam')->where('user_id',input('user_id'))->find())output_error('请勿重复提交');
    	$res = Db::name('exam')->insert($data);

    	Db::name('user')->where('id',input('user_id'))->update(['defen'=>input('defen')]);
    	if($res){
    		output_data('提交成功');
    	}else{
    		output_error('提交失败');
    	}
    }

    public function kaoshitimu(){
    	if(empty(input('user_id')))output_error('请先登录');

        $danxuan = ST::where('type',1)->limit(60)->field('id,A,B,C,D,ctitle,answer,type')->orderRand()->select();
        $duoxuan = ST::where('type',2)->limit(10)->field('id,A,B,C,D,ctitle,answer,type')->orderRand()->select();
        $panduan = ST::where('type',3)->limit(10)->field('id,A,B,C,D,ctitle,answer,type')->orderRand()->select();

    	$shiti = array_merge_recursive($danxuan->toArray(),$duoxuan->toArray(),$panduan->toArray());
    	output_data($shiti);
    }

    public function ceshi(){
    	if(empty(input('user_id')))output_error('请先登录');
    	$shiti = ST::limit(5)->field('id,A,B,C,D,ctitle,answer,type')->orderRand()->select();
    	output_data($shiti);
    }
}
